﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace SmartKeeper
{
    class SortProperty
    {
        public PropertyInfo PropertyInfo { get; set; }
        public bool BigFirst { get; set; }
    }

    class PropertyData
    {
        public PropertyInfo KeyPropertyInfo { get; set; }
        public Dictionary<string, PropertyInfo> UniqueProperties { get; set; }
        public Dictionary<string, PropertyInfo> IndexProperties { get; set; }
        public Dictionary<string, SortProperty> SortProperties { get; set; }

        public PropertyData()
        {
            UniqueProperties = new Dictionary<string, PropertyInfo>();
            IndexProperties = new Dictionary<string, PropertyInfo>();
            SortProperties = new Dictionary<string, SortProperty>();
        }

        public void Reflect(string rootPath, Type type)
        {
            //检查文件目录是否存在
            string dirPath = Path.Combine(rootPath, type.FullName);
            if (!Directory.Exists(dirPath))
            {
                Directory.CreateDirectory(dirPath);
            }

            foreach (PropertyInfo property in type.GetProperties())
            {
                var valueAttr = property.GetCustomAttribute<ValueAttribute>();
                if (valueAttr != null)
                {
                    if (valueAttr.Key)
                    {
                        //key 必须是唯一的
                        UniqueProperties.Add(property.Name, property);
                    }
                    if (valueAttr.IsUnique)
                    {
                        UniqueProperties.Add(property.Name, property);
                    }
                    if (valueAttr.IsIndex)
                    {
                        IndexProperties.Add(property.Name, property);
                    }
                    if (valueAttr.Sortable)
                    {
                        SortProperties.Add(property.Name, new SortProperty()
                        {
                            BigFirst = valueAttr.BigFirst,
                            PropertyInfo = property,
                        });
                    }
                }
            }
        }
    }
}
